<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=8">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta content="B2B电商平台" name="description" />
		<meta content="B2B电商平台" name="keyword" />

		<title>B2B电商平台.商品单位</title>
		<link rel="icon" href="include/images/icss.ico" type="image/x-icon" />
		<link rel="shortcut icon" href="include/images/icss.ico"
			type="image/x-icon" />
		<link rel="stylesheet" type="text/css"
			href="<%=path%>/include/css/global.css" />
		<link rel="stylesheet" type="text/css"
			href="<%=path%>/include/css/style.css" />
		<link rel="stylesheet" type="text/css"
			href="<%=path%>/include/css/pro.css" />
		<link rel="stylesheet" type="text/css"
			href="<%=path%>/include/css/proManage.css" />
		<link rel="stylesheet"
			href="<%=path%>/include/JQuery ui/css/smoothness/jquery-ui-1.7.3.custom.css"
			type="text/css"></link>
		<link rel="stylesheet" type="text/css"
			href="<%=path%>/include/pagination/page.css" media="all">
		<link rel="stylesheet" type="text/css" href="<%=path %>/include/css/mmt_trading.css" />	
		<script type="text/javascript"
			src="<%=path%>/include/js/jquery-1.8.0.min.js"></script>
		<script type="text/javascript" src="<%=path%>/include/js/cookies.js"></script>
		<script type="text/javascript"
			src="<%=path%>/include/js/icss.busin.js"></script>
		<script type="text/javascript"
			src="<%=path%>/include/JQuery ui/js/jquery-ui-1.7.3.custom.min.js"></script>
		<script type="text/javascript"
			src="<%=path%>/include/js/icss.common.js"></script>
		<script type="text/javascript"
			src="<%=path%>/include/pagination/jquery.pagination.js"></script>
		<script type="text/javascript"
			src="<%=path%>/include/pagination/init.js"></script>
		<script type="text/javascript" src="<%=path%>/include/js/doT.min.js"></script>
		<script src="<%=path%>/include/DatePicker/WdatePicker.js" charset="UTF-8" type="text/javascript"></script>
		<style type="text/css">
.table tr td {
	border-left: 1px solid #ddd;
	border-bottom: 1px solid #ddd;
	height: 33px;
}

.table tr th {
	border-left: 1px solid #ddd;
	font-weight: bold;
}
em.create {
    background-position: -50px -44px;
    line-height: 28px;
    display: inline-block;
    font-style: normal;
    background-repeat: no-repeat;
    text-indent: 15px;
    float: right;
    cursor: pointer;
}
.create {
	background: url("<%=path%>/images/coupons/sort_zss.png");
}

#queryId button{
	background-image: url("<%=path%>/include/css/img/xs_bgx.png");
    background-color: transparent;
}
.cxx_btn{
	width: 44px;
    height: 24px;
    background-position: 0px -203px;
    line-height: 24px;
    margin: 0px 0px 0px 5px;
}
a{
	color: #039;
}
#tbody td{
padding: 0px 4px;
vertical-align: middle;
line-height: 20px;
}
.search_lis_se input{
height: 24px;
}
button{
	background-image: url('<%=path %>/include/css/img/xs_bgx.png');
    background-color: transparent;
}
.inp_css{width:100%}
</style>
<script type="text/javascript">
	$(function(){
		//_search();
		_queryList();
	});

	var pagination;
	function _search() {
		var url = "<%=path%>/productPublish/getUnitInfo.do";
		var data = {
				productId :$("#productId").val(),
				unit :$("#unit").val(),
				productName :$("#productName").val()
		};
		pagination = _initPagination({'pageId':'pagination', 'cKey':'roductPublish', 'url': url, 'data': data, 'rows': 10, 'callback':_initDataList});
	}
	
	function _initDataList(data) {
		var d = data.rows;
		var str = "";
		$("#tbody").empty();
		for (var i = 0; i < d.length; i++) {
			str += "<tr>";
			str += "    <td>"+(i+1)+"</td>";
			str += "    <td>"+d[i].productName+"</td>";
			str += "    <td>"+d[i].productId+"</td>";
			str += "    <td>"+d[i].productName+"</td>";
			str += "    <td>"+d[i].unitName+"</td>";
			str += "    <td>"+d[i].buyPrice+"</td>";
			str += "    <td>"+d[i].buyPrice+"</td>";
			str += " </tr>";
		}
		$("#tbody").append(str);
	}

	function _queryList(){
		var productId = $("#productId").val();
		var unit = $("#unit").val();
		var productName = $("#productName").val();
		if(productName.length!=0) productId = "";
		var v = productId+productName+unit;
		if(v=="") return;
		$.ajax({
            type:"post",
            url:"<%=path %>/productPublish/getUnitInfo.do",
            dataType:"json",
            data:{productId:productId,productName:productName,unit:unit},
            success:function(data){
                if(data){
                	var html = "";
	                for(var i=0;i<data.length;i++){
		                var o = data[i];
		                html += "<tr class='paraCss' onclik=\"_changeUnit('"+o.productId+"','"+o.unitName+"')\">";
		                html += "	<td style='text-align: center;'>"+(i+1)+"</td>";
		                html += "	<td>"+o.productName+"</td>";
		                html += "	<td>"+o.productId+"</td>";
		                html += "	<td>"+o.scale+"*"+o.unitName+"</td>";
		                html += "	<td>"+o.unit+"</td>";
		                html += "	<td>"+o.unitName+"</td>";
		                html += "	<td style='text-align: right'>"+o.buyPrice+"</td>";
		                html += "	<td style='text-align: right'>"+o.retailPrice+"</td>";
		                html += "	<td style='text-align: center;border-right: 1px solid #ddd;'><a onclick=\"_save('"+o.unitName+"','"+o.productId+"','"+o.unit+"');\">选择</a></td>";
		                html += "</tr>";
	                }
	                $("#tbody").empty();
	                $("#tbody").append(html);
                }
            }
        });
	}
	function _save(v1,v2,v3){
		parent._setProPubVal(v1,v2,v3);
		var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		parent.layer.close(index);
	}
	
	function _addPara(){
		var len = $(".paraCss").length+1;
		var html = "";
		html += "<tr class='paraCss'>";
        html += "	<td style='text-align: center;'>"+len+"</td>";
        html += "	<td><input class='inp_css inp_"+len+"' id='productName"+len+"' name='productName"+len+"'/></td>";
        html += "	<td><input class='inp_css inp_"+len+"' onkeyup='_verifyNum1(this.id)' id='productId"+len+"' name='productId"+len+"'/></td>";
        html += "	<td><input class='inp_css inp_"+len+"' onkeyup='_verifyNum(this.id)' style='width:60%' id='scale"+len+"' name='scale"+len+"'/><span id='span_"+len+"'></span></td>";
        html += "	<td><select class='inp_css inp_"+len+"' onchange=\"_changeUnitName('"+len+"')\" id='unitName"+len+"' name='unitName"+len+"'><option value=''>请选择</option></select></td>";
        html += "	<td><input class='inp_css inp_"+len+"' disabled='disabled' id='unit"+len+"' name='unit"+len+"'/></td>";
        html += "	<td><input class='inp_css inp_"+len+"' onkeyup='_verifyNum(this.id)' id='buyPrice"+len+"' name='buyPrice"+len+"'/></td>";
        html += "	<td><input class='inp_css inp_"+len+"' onkeyup='_verifyNum(this.id)' id='retailPrice"+len+"' name='retailPrice"+len+"'/></td>";
        html += "	<td style='text-align: center;border-right: 1px solid #ddd;'><a id='submitBtn' onclick=\"_submit('"+len+"');\">提交</a></td>";
        html += "</tr>";
        $("#tbody").append(html);
        _selUnit(len);
	}

	function _change(len){
		var name = $("#unitName"+len).val();
		$("#span_"+len).text("*"+name);
	}

	function _verifyNum(id){
		var str = $("#"+id).val();
		var len = str.length;
		for(var i=len;i>0;i--){
			var v = str.substr(i-1,1);
			if(v==".") continue;
			if(v<"0"||v>"9") $("#"+id).val(str.substr(0,i-1));
		}
	}
	
	function _verifyNum1(id){
		var str = $("#"+id).val();
		var len = str.length;
		for(var i=len;i>0;i--){
			var v = str.substr(i-1,1);
			if(!/^[a-zA-Z0-9]+$/.test(v)) $("#"+id).val(str.substr(0,i-1));
		}
	}
	
	function _submit(len){
		var productId = $("#productId"+len).val();
		var productName = $("#productName"+len).val();
		var scale = $("#scale"+len).val();
		var unit = $("#unit"+len).val();
		var unitName = $("#unitName"+len).val();
		var buyPrice = $("#buyPrice"+len).val();
		var retailPrice = $("#retailPrice"+len).val();
		parent.layer.confirm("要提交新增商品信息吗", function(index){
			$.ajax({
	            type:"post",
	            url:"<%=path %>/productPublish/setUnitInfo.do",
	            dataType:"text",
	            data:{
	                productId:productId,
	                productName:productName,
	                scale:scale,
	                unit:unit,
	                unitName:unitName,
	                buyPrice:buyPrice,
	                retailPrice:retailPrice
	                },
	            success:function(data){
	                    if(data=="1"){
	                        parent.layer.alert("提交成功");
	                        $(".inp_"+len).attr("disabled",true);
							$("#submitBtn").text("选择");
							$("#submitBtn").removeAttr("onclick");
							$("#submitBtn").attr("onclick","_save('"+unitName+"','"+productId+"','"+unit+"')");
	                    }else{
	                        parent.layer.alert("提交失败，请检查数据后重新提交");
	                    }
	            }
			});
			 parent.layer.close(index);
		});
	}

	function _selUnit(len){
		$.ajax({
            type:"post",
            url:"<%=path %>/productPublish/queryUnitList.do",
            dataType:"json",
            data:{},
            success:function(data){
                if(data){
                    var html = "";
                    var html1 = "";
                    for(var i=0;i<data.length;i++){
                        html += "<option value='"+data[i].UNIT_CODE+"'>"+data[i].UNIT_CODE+"</option>";
                        html1 += "<option value='"+data[i].UNIT_NAME+"' unitCode='"+data[i].UNIT_CODE+"'>"+data[i].UNIT_NAME+"</option>";
                    }
                    $("#unit"+len).append(html);
                    $("#unitName"+len).append(html1);
                }
            }
		});
	}

	function _changeUnitName(len){
		var name = $("#unitName"+len).val();
		$("#span_"+len).text("*"+name);
		var code = $("#unitName"+len).find("option:selected").attr("unitCode");
		$("#unit"+len).val(code);
	}
</script>
	</head>

	<body style="font-family: '微软雅黑';">
		<div id="contentDiv" style="padding:4px; width:99%;">
			<div class="colnums-2-ab-b">
				<input type="hidden" id="productId" name="productId" value="${productId }"/>
				<input type="hidden" id="unit" name="unit" value=""/>
				<div id="queryId" style="border: 1px solid #e0e0e0;background: #f5f5f5;padding-left:10px;margin-bottom:10px;height: 40px;;line-height: 40px;font-size:14px;">
					商品名称:&nbsp;<input id="productName" name="productName" value="${productName }"/>
					<button class="cxx_btn" type="button" onclick="_queryList()">查询</button>
					<button class="cxx_btn" type="button" onclick="_addPara()">添加</button>
				</div>
				<div class="row-fluid dataTables_wrapper" style="height: 270px;overflow: auto;">
					<table id="listTable"
						class="table table-hover table-nomargin table-bordered usertable dataTable"
						style="width: 100%; border-spacing: 0;">
						<thead>
							<tr class="trBgd"
								style="background-color: #eee;text-align: center; height: 32px; border: 1px solid #ddd; white-space: nowrap; background-position: 0 -123px; line-height: 32px; font-weight: bold;">
								<th style="width: 5%">
									序号
								</th>
								<th style="width: 25%">
									商品名称
								</th>
								<th style="width: 15%">
									商品条码
								</th>
								<th style="width: 13%">
									商品规格
								</th>
								<th style="width: 8%">
									计量单位
								</th>
								<th style="width: 10%">
									单位编码
								</th>
								<th style="width: 8%">
									批发价格
								</th>
								<th style="width: 8%">
									零售价格
								</th>
								<th style="width: 8%">
									操作
								</th>
							</tr>
						</thead>
						<tbody id="tbody" style="font-size: 13px">
						</tbody>
					</table>
					<div class="dataTables_paginate paging_full_numbers">
						<span id="innerHtml"> </span>
					</div>
					<div id="pagination"></div>
					<div>
						<div style="height: 30px;"></div>
						<!-- 分类列表div结束 -->
						<div class="clear"></div>
					</div>
				</div>
				</div>
			</div>
	</body>
	<script type="text/javascript">
	$(document).ready(function(){
			_reHeight("rightFrame","contentDiv");
	 });
</script>
</html>
